$(function (){
    // 加载商品列表数据并渲染
    $.ajax({
        url: '../data/jmyp.json',
        type: 'get',
        dataType: 'json',
        success: function (data){
            var strDom = ''
           // console.log(goodsArr);
            $.each(data,function (index,item){
               // console.log(item);
                $.each(goodsArr,function (index,obj){
                   // console.log(obj);
                   // console.log(item);
                  // console.log(obj);
                   //console.log(obj.id===item.id);
                if(obj.id===item.id&&obj.key==="ture"){
                    
                

                
                    
                
                var divs = `
                <div class='div1'>
                <div class='leftbox'>
                    <img src="${item.imgurl1}">
                    <div class='mark'></div>
                </div>
                <ul>
                    <li class='border'><img src="${item.imgurl1}"></li>
                    <li><img src="${item.imgurl2}"></li>
                </ul>
                <div class="rightbox">
                    <img src="${item.imgurl1}">
                </div>
            </div>
            <div class="xiangtext">
                <div class="xiangqingtext">
                    <div class="biaoti">
                        <h1 class="title">
                        ${item.title}
                        </h1>
                    </div>
                    <div class="price">
                        ￥<span>${item.price}</span>
                    </div>
                    <div class="timer">
                        <span class="times">00天00时00分</span>
                    </div>
                    <div class="jianjia">
                        <ul>
                            <li>
                                <span>x元y件</span>
                                南医大专利 满2件实付59元,满3件实付85元,满4件实付109元,满5件实付135元
                            </li>
                        </ul>
                    </div>
                    <div class="xuxian">
    
                    </div>
                    <div class="yunfei">
                        <span>运费</span>
                        <span>商家满199包邮</span>
                    </div>
                    <div class="xuxian">
                    </div>
                    <div class="baoyou">
                        <span>服务</span>
                        <ul>
                            <li><a href="#">质量保险</a></li>
                            <li><a href="#">本商品不支持退货</a></li>
                            <li><a href="#">本商品不支持换货</a></li>
                            <li><a href="#">闪电发货</a></li>
                        </ul>
                    </div>
                    <div class="xuxian">
                    </div>
                    <div class="xinghao">
                    <ul>
                        <li>型号</li>
                        <li><a>清爽款</a></li>
                        <li><a>美白款</a></li>
                    </ul>
                </div>
                <div class="num">
                    <ul>
                        <li>数量</li>
                        <li><button class="jian">-</button></li>
                        <li><input class="number" type="text" value="1"></li>
                        <li><button class="jia">+</button></li>
                    </ul>
                </div>
                <div data-id="${item.id}" class="goumai">
                    <a>加入购物车</a>
                </div>
                </div>
            </div>
            
            
            `
                strDom+= divs
                //console.log(item.id);
            
            }
    })
        })
            
            $(".fdj").append(strDom)
            
        }
    })

    if(localStorage.getItem('goods')){
        var goodsArr=JSON.parse(localStorage.getItem('goods'))
    }else{
        var goodsArr=[]
    }

    




     
    $('.fdj').on('click','.jia',function(){
        var num11=$(this).parent().prev().children().val()
        num11++
        $(this).parent().prev().children().val(num11)
    })
        
   

    $('.fdj').on('click','.jian',function(){
        var num22=$(this).parent().next().children().val()
        num22--
        if(num22<=1){
            num22=1
        }
        $(this).parent().next().children().val(num22)
        
    })



    

    $('.fdj').on('click','.goumai',function(){
        var id=$(this).attr('data-id')
      //  console.log(id);
        var flag=false
        $.each(goodsArr,function(index,item){
            if(item.id===id){
                item.num++
                flag=true
                
            }
        })

        if(!flag){
            goodsArr.push({'id':id,'num':1})
        }
        localStorage.setItem('goods',JSON.stringify(goodsArr))
        alert('加入购物车成功！')
        console.log($('.number').val())
    })

    
})


 
setTimeout(function(){
    var timer =document.querySelector('.timer')
var times=document.querySelector('.times')


function sjc(){
    var t1=new Date()
    var t2=new Date("2021-4-30 8:00:00")
    var tt=Math.floor((t2-t1)/1000)
    var day1=Math.floor(tt/60/60/24) 
    var hh=Math.floor((tt-day1*60*60*24)/3600)
    var mm=Math.floor((tt-day1*60*60*24-hh*3600)/60)
    var ss=tt-day1*24*3600-hh*3600-mm*60
    times.innerHTML=("优惠剩余："+day1+"天"+hh+"时"+mm+"分"+ss+"秒")
   //var text=("优惠剩余："+day1+"天"+hh+"时"+mm+"分"+ss+"秒")
   //console.log(times.innerHTML);
   //console.log(text);
   //times.innerHTML=text
}
 sjc()
setInterval(sjc,1000)

},100)

    
